﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using loginSQL.Pages;

namespace loginSQL.SQLHelper
{
    public class SaveLoadConfig
    {
        static string filename = "AppConf.txt";


        public static void Save(MySQLSettings settings,string server_pusat)
        {
            StreamWriter sw = null;     
            try
            {
                sw = new StreamWriter(filename);      
                sw.WriteLine(settings.server);
                sw.WriteLine(settings.port);
                sw.WriteLine(settings.username);
                sw.WriteLine(settings.password);
                sw.WriteLine(settings.databaseName);
                sw.WriteLine(server_pusat);
            }
            catch
            {
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                }
            }                        
        }

        public static MySQLSettings Load()
        {
            MySQLSettings settings = new MySQLSettings("localhost", "3306", "root", "password","3trust_hubud_lisensi_new");
            string server_pusat = "localhost";
            StreamReader sr = null;
            try
            {
                sr = new StreamReader(filename);
                List<string> data = new List<string>();
                while (!sr.EndOfStream)
                {
                    data.Add(sr.ReadLine());
                }
                int diff = data.Count - 6;
                if (diff < 0)
                {
                    File.CreateText(filename);
                    Save(settings,server_pusat);
                    
                }
                else
                {
                    settings = new MySQLSettings(data[0], data[1], data[2], data[3],data[4]);
                    Constants.server_pusat = data[5];
                    
                }

            }
            catch (FileNotFoundException ex)
            {
                File.CreateText(filename);
                Save(settings,server_pusat);
            }
            catch
            {
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                }
            }
            return settings;
        }

    }
}
